<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


/**
 * 个人中心接口
 */
class My extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
        $this->model = new \app\common\model\Order;
    }  

    /**
     * 我的订单 (company：快递公司。delivery_time发货时间。)
     */
    public function order_list(){
      // pay_status 支付状态:10=未支付,20=已支付,30=已发货,40=确认收货
      $return['daifahuo'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime")->where(['user_id'=>$this->user_id,'pay_status'=>20,'spec_type'=>50])->select();
      $return['yifahuo'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,FROM_UNIXTIME(delivery_time,'%Y-%m-%d %H:%i:%s') as delivery_time")->where(['user_id'=>$this->user_id,'pay_status'=>30,'spec_type'=>50])->select();
      $return['queren'] = Db::name('order')->field("*,FROM_UNIXTIME(updatetime,'%Y-%m-%d %H:%i:%s') as updatetime,FROM_UNIXTIME(delivery_time,'%Y-%m-%d %H:%i:%s') as delivery_time")->where(['user_id'=>$this->user_id,'pay_status'=>40,'spec_type'=>50])->select();
      $this->success('我的订单',$return);
    }

    /**
     * 确认收货
     * @param int $order_id
     */
    public function sure_get_order(){
      $order_id = input('order_id');

      $my_order_data = $this->model->where('id',$order_id)->find();
      if(empty($my_order_data))$this->error('该订单信息不存在');

      if($my_order_data['pay_status']=='40')$this->error('该订单已经确认过收货了');

      $my_order_data->isUpdate(true)->save(['pay_status'=>40]);

      $this->success('确认收货成功');
    }

    

  

}
